'use strict';
 
angular.module('hemApp',[]).controller('scenesCtrl', function($scope, $http,$rootScope) {
	
		$http.get(".html")
		.success(function (scenes) {
			  $(document).ready(function($){
				  $("#sub").attr("name",scenes[0].id);
					}(jQuery));
			  $scope.scenes = scenes;
			 window.scenesElements = scenes;
	  });
	  
	  $scope.delScene = function($event){
		      $event.stopPropagation();
		      if(confirm('确定要删除么?')){
		    	  $http.delete(""+this.m.id+".html?") //删除
				  .success(function (data) {
//					  if( typeof(data) != "undefined" &&typeof(data) != "string" )alert(data.name);
					  $http.get(".html")
					  .success(function (scenes){
						  $scope.scenes = scenes;
						  var scenesIdFirst = scenes[0].id;
						  $(document).ready(function($){
							  $("#loding").css("display","block");
								 $(".listOpen>div").remove();
								 $(".listOff>div").remove();
								}(jQuery));
						  $http.get(""+scenesIdFirst+".html")
						  .success(function (scenes) {
							  $rootScope.scenes = scenes;
						  })
						  $rootScope.modules =  scenes[0].elements;
						  
						  $(document).ready(function($){
							  $("#sub").attr("name",scenes[0].id);
//							  $("#modSel").children().first().css({"border":"2px solid red","border-right":"3px double red"});
//							  $("#"+scenes[0].id).css("border","1px solid red");
							  setTimeout(function(){
								  $("#"+scenes[0].id).siblings().css({"border":"1px solid white"});
								  $("#"+scenes[0].id).css({"border":"2px solid red","border-right":"3px double red"});
								  $("#"+scenes[0].id+"_list").removeClass("item-not-fitst").addClass("item-fitst");
								  $("#"+scenes[0].id+"_list").siblings().removeClass("item-fitst").addClass("item-not-fitst");
								  $("#loding").css("display","none");
							  },1000);
//							  $("#sub").attr("name",scenes[0].id);
								}(jQuery));
//						  window.scenesElements = scenes;
					  });
				  }).error(function (err) {
					  alert("删除失败！") ; 
					});
				}
		  	  
			};
			
	  $scope.selScene = function($event){//点击选择模式
//			  Profile.selSceneId = this.m.id;
		  $event.stopPropagation();
			  var selId = this.m.id;
			  $(document).ready(function($){
				  $("#loding").css("display","block");
				  $("#"+selId).siblings().css({"border":"1px solid white"});
				  $("#"+selId).css({"border":"2px solid red","border-right":"3px double red"});
				  $("#"+selId+"_list").removeClass("item-not-fitst").addClass("item-fitst");
				  $("#"+selId+"_list").siblings().removeClass("item-fitst").addClass("item-not-fitst");
				  $("#sub").attr("name",selId);
					}(jQuery));
				$http.get(".html")
				.success(function (scenes) {
					 $(document).ready(function($){
							 $(".listOpen>div").remove();
							 $(".listOff>div").remove();
							}(jQuery));
					 setTimeout(function(){
						 for(var y=0;y<scenes.length;y++){
							 if(scenes[y].id==selId){
								 $rootScope.modules =  scenes[y].elements;
							 }
						 }
							$http.get(""+selId+".html")
							.success(function (scenes) {
								  $rootScope.scenes = scenes;
						  });
					 },10)
					 
					  $(document).ready(function($){
						  setTimeout(function(){
							  $("#loding").css("display","none");
						  },100)
				  
					}(jQuery));
					 window.scenesElements = scenes;
			  });
			
			  }  
	})

.controller('addSceneCtrl', function($scope, $http) {
	  $http.post("Rooms.html")
	  .success(function (rooms) {
		  $scope.rooms = rooms;
	  });
	  

	$scope.addScene = function(){
		window.roomsContents=[];
		$(document).ready(function($){
			for(var k = 0;k<$scope.rooms.length;k++){
				var roomsNum = $scope.rooms[k][0];
				if($("#rooms_"+roomsNum+"").is(':checked')==true){
					roomsContents.push(roomsNum);
				}
			}

			}(jQuery));
		
		$http({
		 url:'.html',
	        method:"POST",
	        data: {
	            "name":$scope.newSceneName, "content":$scope.newSceneContent, "remark":"123456","roomIds":roomsContents
	        }
		}).success(function(){
			$scope.newSceneName=null;
			$scope.newSceneContent=null;
			 $(document).ready(function($){
				 $("#mask").css("display","none");
				 window.location.reload();
				}(jQuery));
		}).error(function(err){
				alert("新增失败！");
		});
	}}
	)
	
//	
//	.controller('editSceneCtrl',function($scope, $http){
//		
//		 $(document).ready(function($){
//			 console.log($("#sub").attr("name"));
//			}(jQuery));
//		
//	})
	
	
	.controller('modCtrl', function($scope, $http,$rootScope) {
//	  $http.get("module.html")
//	  .success(function (modules) {
//		  var modulesAll = modules;
//		  console.log(modules);
//
//	  });
	  $http.get(".html")
	  .success(function (scenesId) {
		  var scenesIdFirst = scenesId[0].id;
		  $rootScope.modules = scenesId[0].elements;
//		  $rootScope.scenes = scenesId;
		  $http.get(""+scenesIdFirst+".html")
		  .success(function (scenes) {
			  $rootScope.scenes = scenes;
		  })
	  })
	  
	})
	
	.controller('subRelCtrl', function($scope, $http,$rootScope) {
		$http.post("Rooms.html")
		  .success(function (rooms) {
			  $scope.rooms = rooms; 
		  });
		$scope.editMod = function(){
			
			 $http.get(".html")
			  .success(function (scenes) {
				  $scope.scenes = scenes;
					 $(document).ready(function($){
						 window.editModId = $("#sub").attr("name");
							var modName = $("#"+editModId).text();
						    $("#edit").css("display","block");
						    $("#edit input[type='text']").val(modName);
						    $("#edit input[type='button']").on('click',function(){
							    $("#edit").css("display","none");
							});
						    $("#edit input[type='checkbox']").prop("checked", false);
						    for(var i = 0;i<$scope.scenes.length;i++){
						    	if(editModId==$scope.scenes[i].id){
						    		if($scope.scenes[i].roomIds!=null){
							    		for(var j=0;j<$scope.scenes[i].roomIds.length;j++){
							    			$("#editRooms_"+$scope.scenes[i].roomIds[j]).prop("checked",true);
							    		}
						    		}
						    	}
						    }
						}(jQuery));
				  
			  });
	
			 
		}
		
		$scope.editScene = function(){
			$http.post("Rooms.html")
			  .success(function (rooms) {
				  $scope.rooms = rooms; 	
			  });
			window.editRoomsContents=[];
			$(document).ready(function($){
				for(var k = 0;k<$scope.rooms.length;k++){
					var roomsNum = $scope.rooms[k][0];
					if($("#editRooms_"+roomsNum+"").is(':checked')==true){
						editRoomsContents.push(roomsNum);
					}
				}
//				window.nameVal = $("#"+editModId+"_list").attr("name");
				window.nameVal = $("#edit input[type='text']").val();
				}(jQuery));
			
//			$http.get(".html",{"name":$scope.nameVal,"roomIds":editRoomsContents})
			$http({//更新
				 url:".html?",
			        method:"post",
			        data: {
			        	"id":editModId,"name":nameVal,"content":"123456", "remark":"123456","roomIds":editRoomsContents
			        }
				})
				.success(function(){
					 $(document).ready(function($){
						 alert("更新成功");
						 $("#edit").css("display","none");
//						 window.location.reload();
						}(jQuery));
					
				}).error(function(){
						alert("更新失败！") ; 
				});
		}
		
		
		
		
		$scope.subMod = function($event){
			 $http.get("module.html")
			  .success(function (modules) {
				  $scope.modules = modules;
				  window.modNum = modules.length;
				  window.mods = modules;			  
				  $(document).ready(function($){
						window.subSceneId = $("#sub").attr("name");
						window.timeVal = $("#"+subSceneId+"_list .listOpen #"+subSceneId+"_1ID").val();
						window.nameVal = $("#"+subSceneId+"_list").attr("name");
						window.typeVal = {};
						window.weekVal = [];
						window.elements = [];
						if($("#"+subSceneId+"_list .listOpen #"+subSceneId+"_1ID").length!=0){
							if(timeVal.length!=0){
//								timeVal = timeVal.substring(11,16);
							}else{
								timeVal="00:00";
							}
							for(var u=1;u<8;u++){
								
								var weekCheckbox = $("#"+subSceneId+"_list .listOpen #"+subSceneId+"_1 #"+subSceneId+"week_"+u+"");
								if(weekCheckbox.prop("checked")==true){
									weekVal.push(u);
								}
								
							}
												
//							weekVal = weekVal.substring(0,weekVal.length-1)+"]";
//							console.log(weekVal)
//							weekVal = eval("("+weekVal+")");
							elements.push({"name":"时钟","type":"1","value":timeVal,"week":weekVal}) ;
							
						}

						for(var i = 2;i<modNum+1;i++){
							
							if($("#"+subSceneId+"_list .listOpen #"+subSceneId+"_"+i+"ID").length!=0){
								typeVal[i] = 0;
							}else{
								typeVal[i] = null;
								continue;
							}
							if(i==4){
								var realyType = $("#"+subSceneId+"_list .listOpen #"+subSceneId+"_"+i+" input:checked").length;
				
								if(realyType==1){
									typeVal[i] = 1;
								}
							}
							
							elements.push({"name":mods[i-1].name,"type":mods[i-1].no,"value":typeVal[i]}) ;
							
						}   			
//						for(var u = 1;u<modNum;u++){
//							elements = elements + '{name:"'+mods[u].name+'",type:"'+mods[u].no+'",value:"'+typeVal[u+1]+'"},'
//						}
//						elements = "["+elements.substring(0,elements.length-1)+"]";
//						window.elementsJson = eval("("+elements+")");

						}(jQuery));
				  if(weekVal.length==0){
						return alert("时钟日期选择不能为空！");
					}
					$http({//提交
						 url:""+subSceneId+".html?",
					        method:"put",
					        data: {
					        	"name":nameVal, "content":"123456", "remark":"123456",
					        	"elements":elements
					        }
						}).success(function(){

								 alert("提交成功");
//								 $http.get(""+subSceneId+".html")
//								 .success(function (scenes) {
//									 $rootScope.scenes = scenes;
//								 })
//							 
							
						}).error(function(){
								alert("提交失败！") ; 
						});
				  
			  });
			
		}
		//发布
		$scope.releaseMod = function(){
			$(document).ready(function($){
				window.subSceneId = $("#sub").attr("name");
				$("#loding").css("display","block");
				}(jQuery));
			$http({
				 url:"deploy/"+subSceneId+".html?",
			        method:"get",
				}).success(function(data){
					window.status = data.error;
					window.statusMsg = data.msg;
					if(status=="0"){
						$(document).ready(function($){
							setTimeout(function(){
								$("#loding").css("display","none");
								$("#"+subSceneId).removeClass('status_0');
								$("#"+subSceneId).addClass('status_1')
								$("#"+subSceneId+"_list>#listOff_"+subSceneId+">h4").next("b").text("发布成功");
							},2000);
							window.location.reload();
							}(jQuery));
					}else if(status=="-1"){
						$(document).ready(function($){
							setTimeout(function(){
								$("#loding").css("display","none");
								alert("发布失败,错误代码：（"+status+"）错误信息：("+statusMsg+")");
								
							},2000)
							}(jQuery));
					}
					else if(status=="-2"){
						$(document).ready(function($){
							setTimeout(function(){
								$("#loding").css("display","none");
								alert("发布失败,错误代码：（"+status+"）错误信息：("+statusMsg+")");
						
							},2000)
							}(jQuery));
					}
					
				}).error(function(err){
						alert("发布失败！");
				});
		}
	})

	
.directive('repeatFinish',['$timeout',function($timeout){  
    return {  
    	
        link:function(scope,element,attr,reController){  
            //在这里用jq操作dom  
        	if(scope.$last == true){
            $timeout(function(){ 
            var lodingEnd =	setInterval(function(){
            		if(typeof(scenesElements)=="undefined"){
                		return false;
            		}else{
            			clearInterval(lodingEnd);

                var openDivNum = $(".listOpen").length;
                var listOpen = $(".listOpen").attr("id");
//                var listOpen = $(".listOpen");
//                for(var j = 0;j<openDivNum;j++){
                	
//                	var listOpenId = listOpen[j].id;
                	
                	  var openDivName = $("#"+listOpen+">div").attr("name");
                      var openDivId = $("#"+listOpen+">div").attr("id");
                      var openDivFirstId = openDivId.substring(0,openDivId.lastIndexOf('_'));
                      var opDivType = openDivId.substring(openDivId.indexOf('_'),999);
                      $("#"+openDivFirstId).css({"border":"2px solid red","border-right":"3px double red"});//第一个模式默认选择
              
                
//                var openDivName = $(".listOpen>div").attr("name");
//                var openDivId = $(".listOpen>div").attr("id");
//                var openDivFirstId = openDivId.substring(0,openDivId.lastIndexOf('_'));
//                var opDivType = openDivId.substring(openDivId.indexOf('_'),999);
              	
//                console.log(openDivNum)
//                for(var a1 = 0;a1<openDivNum;a1++){
//                	console.log($(".listOpen>div"));
//                }
                
//                	console.log(this)
//                	for(var j=0;j<openDivNum;j++){
                	var sonId_1 = $(".listOpen #"+openDivFirstId+"_1").length;
                	var sonId_2 = $(".listOpen #"+openDivFirstId+"_2").length;
                	var sonId_3 = $(".listOpen #"+openDivFirstId+"_3").length;
                	var sonId_4 = $(".listOpen #"+openDivFirstId+"_4").length;
//                	console.log($("#"+openDivFirstId+"_4"))
            
                	if(sonId_1!=0){
//                	if(opDivType=="_1"){
                        var timeType = $("#"+openDivFirstId+"_1").attr("name");
//                        function getYMD(){
//                        	
//                        	var update = new Date();
//                            var FullYear=update.getFullYear();
//                            var Month=update.getMonth()+1;
//                            var Day= update.getDate()
//                            var Hours= update.getHours();
//                            var Minutes= update.getMinutes();
////                            var Sec= addZero(update.getSeconds());     
//
//                    		if(parseInt(Hours)>parseInt(timeType.substring(0,2))){
//                    			Day=parseInt(Day)+1;
//                    			if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12){
//                    				if(Day<=31){
//                    					Day=Day;
//                    				}else{
//                    					Month=Month+1;
//                    					Day=1;
//                    				}
//                    			}else if(Month==2){
//                    				if(Day<=29){
//                    					Day=Day;
//                    				}else{
//                    					Month=Month+1;
//                    					Day=1;
//                    				}
//                    			}else{
//                    				if(Day<=30){
//                    					Day=Day;
//                    				}else{
//                    					Month=Month+1;
//                    					Day=1;
//                    				}
//                    			}
//                    		}else if(parseInt(Hours)==parseInt(timeType.substring(0,2))){
//                    			if(Minutes>parseInt(timeType.substring(3,5))){
//
//                    				Day=parseInt(Day)+1;
//                        			if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12){
//                        				if(Day<=31){
//                        					Day=Day;
//                        				}else{
//                        					Month=Month+1;
//                        					Day=1;
//                        				}
//                        			}else if(Month==2){
//                        				if((FullYear%4==0)&&(FullYear%100!=0)){
//                        					if(Day<=29){
//                            					Day=Day;
//                            				}else{
//                            					Month=Month+1;
//                            					Day=1;
//                            				}
//                        		        }else if(FullYear%400==0){
//                        		        	if(Day<=29){
//                            					Day=Day;
//                            				}else{
//                            					Month=Month+1;
//                            					Day=1;
//                            				}
//                        		        }else {
//                        		        	if(Day<=28){
//                            					Day=Day;
//                            				}else{
//                            					Month=Month+1;
//                            					Day=1;
//                            				}
//                        		        }
//                        				
//                        			}else{
//                        				if(Day<=30){
//                        					Day=Day;
//                        				}else{
//                        					Month=Month+1;
//                        					Day=1;
//                        				}
//                        			}
//                    				
//                    			}
//                    		};
//                    		if(Day<10){
//                    			Day = "0"+Day;
//                    		}
//                    		if(Month<10){
//                    			Month = "0"+Month;
//                    		}
//                   		 var timeYMD=FullYear+"/"+Month+"/"+Day+" ";
//                   		 return timeYMD;
//                        	
//                        }
                		
                        
//                        var timeValue = getYMD(timeType) + timeType;
                        var timeValue = timeType;
                        timeValue = 'value="'+timeValue+'"';
 
//                		var timeCtrl = '<input type="text"  id="'+openDivFirstId+'_1ID" ng-model="work_1" class="alertSource" style="z-index:99;color: black;text-align: center" '+timeValue+'><br/>'+
//                		'<div style="display: inline-block" id="'+openDivFirstId+'_1Dao">剩余时间&nbsp;&nbsp;<strong id="'+openDivFirstId+'_1RemainD"></strong>天<strong id="'+openDivFirstId+'_1RemainH"></strong>时<strong id="'+openDivFirstId+'_1RemainM"></strong>分<strong id="'+openDivFirstId+'_1RemainS"></strong>秒</div>';
                		var timeCtrl = '周一:<input type="checkbox" id="'+openDivFirstId+'week_1" style="vertical-align:text-bottom"/>周二:<input type="checkbox" id="'+openDivFirstId+'week_2" style="vertical-align:text-bottom"/>周三:<input type="checkbox" id="'+openDivFirstId+'week_3" style="vertical-align:text-bottom"/>周四:<input type="checkbox" id="'+openDivFirstId+'week_4" style="vertical-align:text-bottom"/>周五:<input type="checkbox" id="'+openDivFirstId+'week_5" style="vertical-align:text-bottom"/>周六:<input type="checkbox" id="'+openDivFirstId+'week_6" style="vertical-align:text-bottom"/>周日:<input type="checkbox" id="'+openDivFirstId+'week_7" style="vertical-align:text-bottom"/>'+
                		'<input type="text"  id="'+openDivFirstId+'_1ID" ng-model="work_1" class="alertSource" style="z-index:99;color: black;text-align: center;height:35px" '+timeValue+'><br/>';
                       	
                        if (!$("#"+openDivFirstId+"_1ID").attr("id")) {
                		$("#"+openDivFirstId+"_1").css("height","auto").html(timeCtrl);
//                		console.log(selId);
//                		$("#"+selId+"ID").css("display","none");
                		
                		$("#"+openDivFirstId+"_1ID").focus(function(e){
                		    e.preventDefault();
                		    $("#" + openDivFirstId+"_1").attr("draggable","false");
                		});
                		$("#"+openDivFirstId+"_1ID").blur(function(e){
                		    e.preventDefault();
                		    $("#" + openDivFirstId+"_1").attr("draggable","true");
                		});
                		$("#"+openDivFirstId+"_1ID").datetimepicker({
                            datepicker:false,
                            format:'H:i',
                            step:10
                        });
                		
                		for(var thisWeek = 0;thisWeek<(scenesElements.length);thisWeek++){
                            if(openDivFirstId==scenesElements[thisWeek].id){
                            	if(scenesElements[thisWeek].elements[0].week.length!=0){
                            		for(var forWeek=0;forWeek<scenesElements[thisWeek].elements[0].week.length;forWeek++){
                                		$("#"+openDivFirstId+"week_"+scenesElements[thisWeek].elements[0].week[forWeek]).prop("checked","checked");
                                	}
                            	}
                            	if(scenesElements[thisWeek].nextTriggerTime!=null){
                            		$("#" + openDivId).append("<p>下次执行时间："+scenesElements[thisWeek].nextTriggerTime+"</p>");
                            	}
                            }
                		}
                		
//                		$("#"+openDivFirstId+"_1ID").datetimepicker({ //添加时间控件
//                		    formatTime:'H:i',
//                		    formatDate:'d.m.Y',
//                		    defaultDate:'+03.01.1970',
//                		    defaultTime:'10:00',
//                		    timepickerScrollbar:false
//                		});
//                		
//                		//定义倒计时
//                		
//                		function GetRTime(){
//                		
//                		    var setTime = $("#"+openDivFirstId+"_1ID").val();
//                		    var setTimeY = parseInt(setTime.substring(0,4));
//                		    var setTimeM = parseInt(setTime.substring(5,7));
//                		    var setTimeD = parseInt(setTime.substring(8,10));
//                		    var setTimeH = parseInt(setTime.substring(11,13));
//                		    var setTimeS = parseInt(setTime.substring(14,16));
//                		    var startTime = new Date();
//                		    //定义参数可返回当天的日期和时间
//                		    startTime.setFullYear(setTimeY,setTimeM-1,setTimeD);//月份是0-11  对应1-12月
//                		    //调用设置年份
//                		    startTime.setHours(setTimeH);
//                		    //调用设置指定的时间的小时字段
//                		    startTime.setMinutes(setTimeS);
//                		    //调用设置指定时间的分钟字段
//                		    startTime.setSeconds(0);
//                		    //调用设置指定时间的秒钟字段
//                		    startTime.setMilliseconds(0);
//                		    //调用置指定时间的毫秒字段
//                		    var EndTime=startTime.getTime();
//                		    //定义参数可返回距 1970 年 1 月 1 日之间的毫秒数
//                		
//                		
//                		    //定义方法
//                		    var NowTime = new Date();
//                		    //定义参数可返回当天的日期和时间
//                		    var nMS = EndTime - NowTime.getTime();
//                		    //定义参数 EndTime减去NowTime参数获得返回距 1970 年 1 月 1 日之间的毫秒数
//                		    var nD = Math.floor(nMS/(1000 * 60 * 60 * 24));
//                		    //定义参数 获得天数
//                		    var nH = Math.floor(nMS/(1000*60*60)) % 24;
//                		    //定义参数 获得小时
//                		    var nM = Math.floor(nMS/(1000*60)) % 60;
//                		    //定义参数 获得分钟
//                		    var nS = Math.floor(nMS/1000) % 60;
//                		    //定义参数 获得秒钟
//                		    if (nMS < 0){
//                		        //如果秒钟大于0
//                		        $("#"+openDivFirstId+"_1Dao").hide();
//                		        //获得天数隐藏
//                		    }else{
//                		        //否则
//                		        $("#"+openDivFirstId+"_1Dao").show();
//                		        //天数展开
//                		        $("#"+openDivFirstId+"_1RemainD").text(nD);
//                		        //显示天数
//                		        $("#"+openDivFirstId+"_1RemainH").text(nH);
//                		        //显示小时
//                		        $("#"+openDivFirstId+"_1RemainM").text(nM);
//                		        //显示分钟
//                		        $("#"+openDivFirstId+"_1RemainS").text(nS);
//                		        //显示秒钟
//                		    }
//                		    if (!$("#"+openDivFirstId+"_1ID")){
//                		        return clearInterval(timer_rt);
//                		    }
//                		}
//                		if (GetRTime){
//                		    window.timer_rt = setInterval(GetRTime,1000);
//                		}
//                		$("#"+openDivFirstId+"_1ID").change(function(){
//                		    clearInterval(timer_rt);
//                		    if (GetRTime){
//                		    	window.timer_rt = setInterval(GetRTime,1000);
//                		    }
//                		})
//                		}
                	}}
                	/*
                	if(sonId_2!=0){
                	
                		var toggleAlert ='<div id="'+openDivFirstId+'_2ID" style="color: black;margin-top: 0;border-top: 1px solid #eee;height: auto">'+
    	                '<label class="label">G102:</label>'+
    	                '<input type="checkbox" name="G102" id="'+openDivFirstId+'_2_G102" style="vertical-align: middle">'+
    	                '<label class="label">G103:</label>'+
    	                '<input type="checkbox" name="G103" id="'+openDivFirstId+'_2_G103" style="vertical-align: middle">'+
    	                '<label class="label">G104:</label>'+
    	                '<input type="checkbox" name="G104" id="'+openDivFirstId+'_2_G104" style="vertical-align: middle">'+
    	                '<label class="label">G105:</label>'+
    	                '<input type="checkbox" name="G105" id="'+openDivFirstId+'_2_G105" style="vertical-align: middle">'+
    	                '<label class="label">G201:</label>'+
    	                '<input type="checkbox" name="G201" id="'+openDivFirstId+'_2_G201" style="vertical-align: middle"><br/>'+
    	                '<label class="label">G202:</label>'+
    	                '<input type="checkbox" name="G202" id="'+openDivFirstId+'_2_G202" style="vertical-align: middle">'+
    	                '<label class="label">G203:</label>'+
    	                '<input type="checkbox" name="G203" id="'+openDivFirstId+'_2_G203" style="vertical-align: middle">'+
    	                '</div>';
//    	                        console.log($("#"+selId).parent().child(" #"+selId+"ID").attr("id"));
    	            $("#"+openDivFirstId+"_2ID").remove(); 
    	            if (!$("#"+openDivFirstId+"_2ID").attr("id")){
    	                $("#"+openDivFirstId+"_2").after(toggleAlert);
    	                $("#"+openDivFirstId+"_2ID").css("display","none");
    	            }
                	}
                	
                	if(sonId_3!=0){
                    	
                		var toggleAlert ='<div id="'+openDivFirstId+'_3ID" style="color: black;margin-top: 0;border-top: 1px solid #eee;height: auto">'+
    	                '<label class="label">G102:</label>'+
    	                '<input type="checkbox" name="G102" id="'+openDivFirstId+'_3_G102" style="vertical-align: middle">'+
    	                '<label class="label">G103:</label>'+
    	                '<input type="checkbox" name="G103" id="'+openDivFirstId+'_3_G103" style="vertical-align: middle">'+
    	                '<label class="label">G104:</label>'+
    	                '<input type="checkbox" name="G104" id="'+openDivFirstId+'_3_G104" style="vertical-align: middle">'+
    	                '<label class="label">G105:</label>'+
    	                '<input type="checkbox" name="G105" id="'+openDivFirstId+'_3_G105" style="vertical-align: middle">'+
    	                '<label class="label">G201:</label>'+
    	                '<input type="checkbox" name="G201" id="'+openDivFirstId+'_3_G201" style="vertical-align: middle"><br/>'+
    	                '<label class="label">G202:</label>'+
    	                '<input type="checkbox" name="G202" id="'+openDivFirstId+'_3_G202" style="vertical-align: middle">'+
    	                '<label class="label">G203:</label>'+
    	                '<input type="checkbox" name="G203" id="'+openDivFirstId+'_3_G203" style="vertical-align: middle">'+
    	                '</div>';
//    	                        console.log($("#"+selId).parent().child(" #"+selId+"ID").attr("id"));
    	            $("#"+openDivFirstId+"_3ID").remove(); 
    	            if (!$("#"+openDivFirstId+"_3ID").attr("id")){
    	                $("#"+openDivFirstId+"_3").after(toggleAlert);
    	                $("#"+openDivFirstId+"_3ID").css("display","none");
    	            }
                	}*/
                	if(sonId_4!=0){
//                		console.log(openDivName+"*****4");
                		var realtValue = $("#"+openDivFirstId+"_4").attr("name");
                		if(realtValue==1){
                			realtValue = 'checked=""';
                		}else{
                			realtValue="";
                		}
                		var toogleRealy = '继电器&nbsp;&nbsp;&nbsp;&nbsp;开关:&nbsp;<input id="'+openDivFirstId+'_4ID" type="checkbox" name="realy" style="vertical-align: text-bottom;" '+realtValue+'/>'
    	        		$("#"+openDivFirstId+"_4").html(toogleRealy);
                	};
//                	}
                	
////////////////////////////////////////点击切换/////////////////////////////////////////////////////////                	
 /*               	$("#modSel>li").on('click',function(){
                		var modSelId = this.getAttribute("id");
                		
                		
                		var openDivNum = $(".listOpen").length;
                        var listOpen = modSelId;
//                        var listOpen = $(".listOpen");
//                        for(var j = 0;j<openDivNum;j++){
                        	
//                        	var listOpenId = listOpen[j].id;
                        	
                        	  var openDivName = $("#listOpen_"+listOpen+">div").attr("name");
                              var openDivId = $("#listOpen_"+listOpen+">div").attr("id");
                              if(openDivId==null){
                            	  return false;
                              }
                              var openDivFirstId = openDivId.substring(0,openDivId.lastIndexOf('_'));
                              var opDivType = openDivId.substring(openDivId.indexOf('_'),999);
                        	
                      
                        
//                        var openDivName = $(".listOpen>div").attr("name");
//                        var openDivId = $(".listOpen>div").attr("id");
//                        var openDivFirstId = openDivId.substring(0,openDivId.lastIndexOf('_'));
//                        var opDivType = openDivId.substring(openDivId.indexOf('_'),999);
                      	
//                        console.log(openDivNum)
//                        for(var a1 = 0;a1<openDivNum;a1++){
//                        	console.log($(".listOpen>div"));
//                        }
                        
//                        	console.log(this)
//                        	for(var j=0;j<openDivNum;j++){
                        	var sonId_1 = $(".listOpen #"+openDivFirstId+"_1").length;
                        	var sonId_2 = $(".listOpen #"+openDivFirstId+"_2").length;
                        	var sonId_3 = $(".listOpen #"+openDivFirstId+"_3").length;
                        	var sonId_4 = $(".listOpen #"+openDivFirstId+"_4").length;
//                        	console.log($("#"+openDivFirstId+"_4"))
                  
                        	if(sonId_1!=0){
                                var timeType = $("#"+openDivFirstId+"_1").attr("name");
//                                function getYMD(){
//                                	
//                                	var update = new Date();
//                                    var FullYear=update.getFullYear();
//                                    var Month=update.getMonth()+1;
//                                    var Day= update.getDate()
//                                    var Hours= update.getHours();
//                                    var Minutes= update.getMinutes();
////                                    var Sec= addZero(update.getSeconds());     
//
//                            		if(parseInt(Hours)>parseInt(timeType.substring(0,2))){
//                            			Day=parseInt(Day)+1;
//                            			if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12){
//                            				if(Day<=31){
//                            					Day=Day;
//                            				}else{
//                            					Month=Month+1;
//                            					Day=1;
//                            				}
//                            			}else if(Month==2){
//                            				if(Day<=29){
//                            					Day=Day;
//                            				}else{
//                            					Month=Month+1;
//                            					Day=1;
//                            				}
//                            			}else{
//                            				if(Day<=30){
//                            					Day=Day;
//                            				}else{
//                            					Month=Month+1;
//                            					Day=1;
//                            				}
//                            			}
//                            		}else if(parseInt(Hours)==parseInt(timeType.substring(0,2))){
//                            			if(Minutes>parseInt(timeType.substring(3,5))){
//
//                            				Day=parseInt(Day)+1;
//                                			if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12){
//                                				if(Day<=31){
//                                					Day=Day;
//                                				}else{
//                                					Month=Month+1;
//                                					Day=1;
//                                				}
//                                			}else if(Month==2){
//                                				if((FullYear%4==0)&&(FullYear%100!=0)){
//                                					if(Day<=29){
//                                    					Day=Day;
//                                    				}else{
//                                    					Month=Month+1;
//                                    					Day=1;
//                                    				}
//                                		        }else if(FullYear%400==0){
//                                		        	if(Day<=29){
//                                    					Day=Day;
//                                    				}else{
//                                    					Month=Month+1;
//                                    					Day=1;
//                                    				}
//                                		        }else {
//                                		        	if(Day<=28){
//                                    					Day=Day;
//                                    				}else{
//                                    					Month=Month+1;
//                                    					Day=1;
//                                    				}
//                                		        }
//                                				
//                                			}else{
//                                				if(Day<=30){
//                                					Day=Day;
//                                				}else{
//                                					Month=Month+1;
//                                					Day=1;
//                                				}
//                                			}
//                            				
//                            			}
//                            		};
//                            		if(Day<10){
//                            			Day = "0"+Day;
//                            		}
//                            		if(Month<10){
//                            			Month = "0"+Month;
//                            		}
//                           		 var timeYMD=FullYear+"/"+Month+"/"+Day+" ";
//                           		 return timeYMD;
//                                	
//                                }
//                                var timeValue = getYMD(timeType) + timeType;
                                var timeValue = timeType;
                                timeValue = 'value="'+timeValue+'"';
                                
                                
                        		var timeCtrl = '周一:<input type="checkbox" id="'+openDivFirstId+'week_1" style="vertical-align:text-bottom"/>周二:<input type="checkbox" id="'+openDivFirstId+'week_2" style="vertical-align:text-bottom"/>周三:<input type="checkbox" id="'+openDivFirstId+'week_3" style="vertical-align:text-bottom"/>周四:<input type="checkbox" id="'+openDivFirstId+'week_4" style="vertical-align:text-bottom"/>周五:<input type="checkbox" id="'+openDivFirstId+'week_5" style="vertical-align:text-bottom"/>周六:<input type="checkbox" id="'+openDivFirstId+'week_6" style="vertical-align:text-bottom"/>周日:<input type="checkbox" id="'+openDivFirstId+'week_7" style="vertical-align:text-bottom"/>'+
                        		'<input type="text"  id="'+openDivFirstId+'_1ID" ng-model="work_1" class="alertSource" style="z-index:99;color: black;text-align: center;height:35px" '+timeValue+'><br/>';
                        		if (!$("#"+openDivFirstId+"_1ID").attr("id")) {
                        		$("#" + openDivId).css("height","auto").html(timeCtrl);
//                        		            $("#"+selId+"ID").css("display","none");
                        		$("#"+openDivFirstId+"_1ID").focus(function(e){
                        		    e.preventDefault();
                        		    $("#" + openDivFirstId+"_1").attr("draggable","false");
                        		});
                        		$("#"+openDivFirstId+"_1ID").blur(function(e){
                        		    e.preventDefault();
                        		    $("#" + openDivFirstId+"_1").attr("draggable","true");
                        		});
                        		$("#"+openDivFirstId+"_1ID").datetimepicker({
                                    datepicker:false,
                                    format:'H:i',
                                    step:10
                                });
                        		for(var thisWeek = 0;thisWeek<(scenesElements.length);thisWeek++){
                                    if(openDivFirstId==scenesElements[thisWeek].id){
                                    	if(scenesElements[thisWeek].elements[0].week.length!=0){
                                    	for(var forWeek=0;forWeek<scenesElements[thisWeek].elements[0].week.length;forWeek++){
                                    		$("#"+openDivFirstId+"week_"+scenesElements[thisWeek].elements[0].week[forWeek]).prop("checked","checked");
                                    	}
                                    	}
                                    	if(scenesElements[thisWeek].nextTriggerTime!=null){
                                    		$("#" + openDivId).append("<p>下次执行时间："+scenesElements[thisWeek].nextTriggerTime+"</p>");
                                    	}
                                    }
                        		}
                        		
//                        		$("#"+openDivFirstId+"_1ID").datetimepicker({ //添加时间控件
//                        		    formatTime:'H:i',
//                        		    formatDate:'d.m.Y',
//                        		    defaultDate:'+03.01.1970',
//                        		    defaultTime:'10:00',
//                        		    timepickerScrollbar:false
//                        		});
////                        		
////                        		//定义倒计时
////                        		
//                        		function GetRTime(){
//                        		
//                        		    var setTime = $("#"+openDivFirstId+"_1ID").val();
//                        		    var setTimeY = parseInt(setTime.substring(0,4));
//                        		    var setTimeM = parseInt(setTime.substring(5,7));
//                        		    var setTimeD = parseInt(setTime.substring(8,10));
//                        		    var setTimeH = parseInt(setTime.substring(11,13));
//                        		    var setTimeS = parseInt(setTime.substring(14,16));
//                        		    var startTime = new Date();
//                        		    //定义参数可返回当天的日期和时间
//                        		    startTime.setFullYear(setTimeY,setTimeM-1,setTimeD);//月份是0-11  对应1-12月
//                        		    //调用设置年份
//                        		    startTime.setHours(setTimeH);
//                        		    //调用设置指定的时间的小时字段
//                        		    startTime.setMinutes(setTimeS);
//                        		    //调用设置指定时间的分钟字段
//                        		    startTime.setSeconds(0);
//                        		    //调用设置指定时间的秒钟字段
//                        		    startTime.setMilliseconds(0);
//                        		    //调用置指定时间的毫秒字段
//                        		    var EndTime=startTime.getTime();
//                        		    //定义参数可返回距 1970 年 1 月 1 日之间的毫秒数
//                        		
//                        		
//                        		    //定义方法
//                        		    var NowTime = new Date();
//                        		    //定义参数可返回当天的日期和时间
//                        		    var nMS = EndTime - NowTime.getTime();
//                        		    //定义参数 EndTime减去NowTime参数获得返回距 1970 年 1 月 1 日之间的毫秒数
//                        		    var nD = Math.floor(nMS/(1000 * 60 * 60 * 24));
//                        		    //定义参数 获得天数
//                        		    var nH = Math.floor(nMS/(1000*60*60)) % 24;
//                        		    //定义参数 获得小时
//                        		    var nM = Math.floor(nMS/(1000*60)) % 60;
//                        		    //定义参数 获得分钟
//                        		    var nS = Math.floor(nMS/1000) % 60;
//                        		    //定义参数 获得秒钟
//                        		    if (nMS < 0){
//                        		        //如果秒钟大于0
//                        		        $("#"+openDivFirstId+"_1Dao").hide();
//                        		        //获得天数隐藏
//                        		    }else{
//                        		        //否则
//                        		        $("#"+openDivFirstId+"_1Dao").show();
//                        		        //天数展开
//                        		        $("#"+openDivFirstId+"_1RemainD").text(nD);
//                        		        //显示天数
//                        		        $("#"+openDivFirstId+"_1RemainH").text(nH);
//                        		        //显示小时
//                        		        $("#"+openDivFirstId+"_1RemainM").text(nM);
//                        		        //显示分钟
//                        		        $("#"+openDivFirstId+"_1RemainS").text(nS);
//                        		        //显示秒钟
//                        		    }
//                        		    if (!$("#"+openDivFirstId+"_1ID")){
//                        		        return clearInterval(timer_rt);
//                        		    }
//                        		}
//                        		if (GetRTime){
//                        		    window.timer_rt = setInterval(GetRTime,1000);
//                        		}
//                        		$("#"+openDivFirstId+"_1ID").change(function(){
//                        		    clearInterval(timer_rt);
//                        		    if (GetRTime){
//                        		    	window.timer_rt = setInterval(GetRTime,1000);
//                        		    }
//                        		})
//                        		}
                        	}}
                        	if(sonId_2!=0){
                        	
                        		var toggleAlert ='<div id="'+openDivFirstId+'_2ID" style="color: black;margin-top: 0;border-top: 1px solid #eee;height: auto">'+
            	                '<label class="label">G102:</label>'+
            	                '<input type="checkbox" name="G102" id="'+openDivFirstId+'_2_G102" style="vertical-align: middle">'+
            	                '<label class="label">G103:</label>'+
            	                '<input type="checkbox" name="G103" id="'+openDivFirstId+'_2_G103" style="vertical-align: middle">'+
            	                '<label class="label">G104:</label>'+
            	                '<input type="checkbox" name="G104" id="'+openDivFirstId+'_2_G104" style="vertical-align: middle">'+
            	                '<label class="label">G105:</label>'+
            	                '<input type="checkbox" name="G105" id="'+openDivFirstId+'_2_G105" style="vertical-align: middle">'+
            	                '<label class="label">G201:</label>'+
            	                '<input type="checkbox" name="G201" id="'+openDivFirstId+'_2_G201" style="vertical-align: middle"><br/>'+
            	                '<label class="label">G202:</label>'+
            	                '<input type="checkbox" name="G202" id="'+openDivFirstId+'_2_G202" style="vertical-align: middle">'+
            	                '<label class="label">G203:</label>'+
            	                '<input type="checkbox" name="G203" id="'+openDivFirstId+'_2_G203" style="vertical-align: middle">'+
            	                '</div>';
//            	                        console.log($("#"+selId).parent().child(" #"+selId+"ID").attr("id"));
            	            $("#"+openDivFirstId+"_2ID").remove(); 
            	            if (!$("#"+openDivFirstId+"_2ID").attr("id")){
            	                $("#"+openDivFirstId+"_2").after(toggleAlert);
            	                $("#"+openDivFirstId+"_2ID").css("display","none");
            	            }
                        	}
                        	
                        	if(sonId_3!=0){
                            	
                        		var toggleAlert ='<div id="'+openDivFirstId+'_3ID" style="color: black;margin-top: 0;border-top: 1px solid #eee;height: auto">'+
            	                '<label class="label">G102:</label>'+
            	                '<input type="checkbox" name="G102" id="'+openDivFirstId+'_3_G102" style="vertical-align: middle">'+
            	                '<label class="label">G103:</label>'+
            	                '<input type="checkbox" name="G103" id="'+openDivFirstId+'_3_G103" style="vertical-align: middle">'+
            	                '<label class="label">G104:</label>'+
            	                '<input type="checkbox" name="G104" id="'+openDivFirstId+'_3_G104" style="vertical-align: middle">'+
            	                '<label class="label">G105:</label>'+
            	                '<input type="checkbox" name="G105" id="'+openDivFirstId+'_3_G105" style="vertical-align: middle">'+
            	                '<label class="label">G201:</label>'+
            	                '<input type="checkbox" name="G201" id="'+openDivFirstId+'_3_G201" style="vertical-align: middle"><br/>'+
            	                '<label class="label">G202:</label>'+
            	                '<input type="checkbox" name="G202" id="'+openDivFirstId+'_3_G202" style="vertical-align: middle">'+
            	                '<label class="label">G203:</label>'+
            	                '<input type="checkbox" name="G203" id="'+openDivFirstId+'_3_G203" style="vertical-align: middle">'+
            	                '</div>';
//            	                        console.log($("#"+selId).parent().child(" #"+selId+"ID").attr("id"));
            	            $("#"+openDivFirstId+"_3ID").remove(); 
            	            if (!$("#"+openDivFirstId+"_3ID").attr("id")){
            	                $("#"+openDivFirstId+"_3").after(toggleAlert);
            	                $("#"+openDivFirstId+"_3ID").css("display","none");
            	            }
                        	}
                        	
                        	if(sonId_4!=0){
//                        		console.log(openDivName+"*****4");
                        		var realtValue = $("#"+openDivFirstId+"_4").attr("name");
                        		if(realtValue==1){
                        			realtValue = 'checked=""';
                        		}else{
                        			realtValue="";
                        		}
                        		var toogleRealy = '继电器&nbsp;&nbsp;&nbsp;&nbsp;开关:&nbsp;<input id="'+openDivFirstId+'_4ID" type="checkbox" name="realy" style="vertical-align: text-bottom;" '+realtValue+'/>'
            	        		$("#"+openDivFirstId+"_4").html(toogleRealy);
                        	};
                		

                	});*/
                	
                	     	
                
    		}
        	},10);        
//                }
            },0); 
            
        	}
        }
  
    };  
}]);  